package com.jdbctest.training.crud;

import com.jdbctest.numbertwo.JDBCUtils;
import com.jdbctest.training.bean.Customer;
import org.junit.Test;

import java.lang.reflect.Field;
import java.sql.*;

public class CustomerForQuery {

    @Test
    public void testQueryForCustomers() {
        String sql = "select id,name,birth,email from customer where id = ?";
        Customer customer = queryForCustomers(sql, 13);

        System.out.println(customer);

        sql = "select name,email from customers where name = ?";
        Customer customer1 = queryForCustomers(sql, "周杰伦");
        System.out.println(customer1);
    }

    /**
     * 针对于customers表的通用的where查询操作
     *
     * @param sql
     * @param args
     * @return
     */
    public Customer queryForCustomers(String sql, Object... args) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            connection = JDBCUtils.getConnection();

            preparedStatement = connection.prepareStatement(sql);

            for (int i = 0; i < args.length; i++) {
                preparedStatement.setObject(i + 1, args[i]);
            }

            resultSet = preparedStatement.executeQuery(sql);

            //获取结果集的元数据: ResultSetMetaData
            ResultSetMetaData metaData = resultSet.getMetaData();
            //通过ResultSetMetaData获取结果集中的列数
            int columnCount = metaData.getColumnCount();

            if (resultSet.next()) {
                Customer customer = new Customer();
                //处理结果集一行数据中的每一列
                for (int i = 0; i < columnCount; i++) {
                    //获取列值
                    Object object = resultSet.getObject(i + 1);
                    //获取每个列的列名
                    String columnLabel = metaData.getColumnLabel(i + 1);
                    //给cust对象指定的columnName属性,赋值为columValue: 通过反射
                    Field field = Customer.class.getDeclaredField(columnLabel);
                    field.setAccessible(true);
                    field.set(customer, object);
                }
                return customer;
            }


        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(connection, preparedStatement, resultSet);

        }

        return null;
    }

    @Test
    public void testQuery1() {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            connection = JDBCUtils.getConnection();
            String sql = "select id,name,email,birth from customers where id = ?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setObject(1, 1);
            //执行,并返回结果集
            resultSet = preparedStatement.executeQuery();
            //处理结果集
            //next():判断结果集的下一条是否有数据，如果有数据返回true,并且指针下移，如果返回false,指针不会下移
            if (resultSet.next()) {
                //获取当前这条数据的各个字段
                int id = resultSet.getInt(1);
                String name = resultSet.getString(2);
                String email = resultSet.getString(3);
                Date birth = resultSet.getDate(4);

                //方式一：
                // System.out.println("id = " + id + ",name = " + name + ",email = " + email + ",birth = " + birth);

                //方式二:
                Object[] data = new Object[]{id, name, email, birth};

                //方式三:
                Customer customer = new Customer(id, name, email, birth);
                System.out.println(customer);

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //关闭资源
            JDBCUtils.closeResource(connection, preparedStatement, resultSet);
        }
    }
}
